package com.intelligent.eayon.coder;

import com.intelligent.eayon.message.TransMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransMsgEncoder extends MessageToByteEncoder {

    private static Logger logger = LoggerFactory.getLogger(TransMsgDecoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {

        TransMessage message = (TransMessage) msg;

        out.writeBytes(message.getHead().getVern());
        out.writeBytes(message.getHead().getSeqn());
        out.writeByte(message.getHead().getCtnf());

        if (message.getHead().getCtnf() != 0) {
            out.writeBytes(message.getHead().getPakn());
        }

        out.writeBytes(message.getHead().getTimp());
        out.writeBytes(message.getHead().getMsid());
        out.writeBytes(message.getHead().getErrc());
        out.writeBytes(message.getHead().getMspr());
        out.writeBytes(message.getHead().getMslg());
        out.writeByte(message.getHead().getHcs());

        if (message.getBody().getData() != null) {
            out.writeBytes(message.getBody().getData());
        }
        out.writeByte(message.getBody().getDcs());

    }
}
